/* eslint valid-jsdoc: "off" */

'use strict';

const db = require("./database");

/**
 * @param {Egg.EggAppInfo} appInfo app info
 */
module.exports = appInfo => {
  /**
   * built-in config
   * @type {Egg.EggAppConfig}
   **/
  const config = (exports = {});

  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + 'app_key_secret';

  // add your middleware config here
  config.middleware = ['logger'];

  // 数据库链接
  config.sequelize = db;

  // PayPal configuration
  config.paypal = {
    clientId: 'AZeTyR3oqVmNGVs9yBdi6IZ58Os-WUDnu6w601SkqLES8WU7z9v7HmWFlU7tE1c7VXg538dmKrhWcFlj',
    clientSecret: 'EN5eYTmWvTDhMsVrrJ2Uyj1_BEzGtO5797i9FugptLjKMaYO1QbYgu1awTG7RSXV2UrQf9BVVgdwh-9C',
    sandbox: true
  };
  // Alipay configuration
  config.antom = {
    url: 'https://open-sea-global.alipay.com',
    clientId: 'SANDBOX_5YC48U2ZQM7808468',
    privateKey: '-----BEGIN PRIVATE KEY-----\n' +
        'MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCRbY2YoVA8rS4DPQsVoBHx4e2LK9Ua7SMrvBfAQe+JpYwpdKM45F05Ia3aBOfO5yGyaOLwtd7NqqLPKJX9Dt3bDnEfHZYW54QThph7J15lbdfouZnS3dFxSmSUqab9OyjAbIGimwRcU5x9jjXi8i0eom6/s6uzyUJRjP0A0zrgUfI7IULK9A0bu/hR0KQb2mOmaOpt7ltldHHVQjg9WVl8oi1GN6UU8M90VUMMa+0WP7yVWe9IJ2i8XoYMpuzd6KOzP+dz9fEJixtFusJojJESEVuSpZ7od8LYBMnm/Lxz3zMWmVT0JF7n68G8baFpyGq0w704rTh/diZ/H7i7fDIPAgMBAAECggEAD2Am1VlQ02l38O22S04GN62sYMd8m4Ew88HKPRB7k/EF1ag4TZ6PYKf36pIUNCLpxCQkwlBoQxRkbESdPwkgWbW4LhMBuGA37ZyQaIzsBQtNBZUB93JuX2z/H1ykhCeKEnYmapu7JUv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5yY9uLMA3pA==\n' +
        '-----END PRIVATE KEY-----',
    clientUrl: 'https://www.tasyachttrip.com.au'
  }

  // 不开启 csrf 验证
  config.security = {
    csrf: {
      enable: false,
    },
  };

  // 允许跨域
  config.cors = {
    // origin: ['http://localhost:8080'],
    origin: '*',
    keepHeadersOnError: true,
    allowMethods: 'GET,HEAD,POST,PUT,DELETE,PATCH,OPTIONS',
    allowHeaders: [
      'Accept',
      'Content-Type',
      'Client-Signature',
      'User-Token',
      'x-requested-with',
    ],
  };

  // 文件上传配置
  config.multipart = {
    fileSize: '50mb',
    whitelist: ['.jpg', '.jpeg', '.png', '.gif', '.mov', '.mp4', '.pdf'],
  };

  // 异常处理
  config.onerror = {
    // 代码异常时, 执行这个处理函数, 其他函数不会生效
    // all(){}

    // 当请求头中没有: Accept: application/json 时执行
    html(e, ctx) {
      ctx.body = `<h1>服务端报错了...</h1><p>${e.message}<p>`;
    },

    // 当请求头中有: Accpet: application/json 时候执行, 用于接口的错误处理
    json(e, ctx) {
      // eslint-disable-next-line no-proto
      if (e.__proto__.constructor.name === 'UnprocessableEntityError') {
        return ctx.error(422, e.errors[0].message); // 处理 egg-validate 抛的异常
      }
      return ctx.error(500, e.message);
    },
  };

  // 自定义配置
  const userConfig = {
    // myAppName: 'egg',
    imgHost: 'http://localhost:7001',
  };

  return { ...config, ...userConfig };
};
